﻿using System;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;

namespace Code5Systems.ClientFramework
{
	public class Helpers
	{
		public static string GetWeightDisplay(decimal weight)
		{
			string dispalyVal = weight.ToString();
			try
			{
				dispalyVal = String.Format("{0:0.0000}", weight);
			}
			catch { }

			return dispalyVal;
		}

		public static String GetFileVersion()
		{
			string fileVersion = String.Empty;
			var theAssembly = Assembly.GetCallingAssembly();
			if (theAssembly == null)
			{
				theAssembly = Assembly.GetExecutingAssembly();
			}
			object[] attributes = theAssembly.GetCustomAttributes(typeof(AssemblyFileVersionAttribute), false);

			if (attributes.Length != 0)
			{
				fileVersion = ((AssemblyFileVersionAttribute)attributes[0]).Version.ToString();
			}

			return fileVersion;
		}

		public static string GetRandomKey(int bytelength)
		{
			byte[] buff = new byte[bytelength];
			RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
			rng.GetBytes(buff);
			StringBuilder sb = new StringBuilder(bytelength * 2);
			for (int i = 0; i < buff.Length; i++)
				sb.Append(string.Format("{0:X2}", buff[i]));
			return sb.ToString().ToLower();
		}

		public static string Encrypt(string toEncrypt)
		{
			if (String.IsNullOrEmpty(toEncrypt))
			{
				throw new ArgumentException("Value cannot be null or empty", "toEncrypt");
			}
			RijndaelCryptoHelper encHelper = new RijndaelCryptoHelper();
			return encHelper.Encrypt(toEncrypt);
		}

		public static string Decrypt(string toDecrypt)
		{
			if (String.IsNullOrEmpty(toDecrypt))
			{
				throw new ArgumentException("Value cannot be null or empty", "toDecrypt");
			}
			RijndaelCryptoHelper encHelper = new RijndaelCryptoHelper();
			return encHelper.Decrypt(toDecrypt);
		}

		public static byte GetProductLevel()
		{
			return 4;
		}
	}
}
